﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlotasDesktop;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.RegistrarViaje
{
    public partial class RegistrarViajes : Form
    {
        String conexion;
        public RegistrarViajes()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
            this.cargarClientes();
            this.cargarChofer();
            this.cargarTurnos();
            tipoViajeComboBox.SelectedIndex = 0;
            fechaHoraPicker.CustomFormat = "dd/MM/yyyy HH:mm:ss";
            fechaHoraPicker.Format = DateTimePickerFormat.Custom;
        }

        private void cargarTurnos()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_turnoHabilitado";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            turnoComboBox.DataSource = dt;
                            turnoComboBox.DisplayMember = "Descripcion";
                            turnoComboBox.ValueMember = "id";                         

                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void cargarClientes(){
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_clienteHabilitado";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            DataColumn dcFullName = new DataColumn();
                            dcFullName.DataType = System.Type.GetType("System.String");
                            dcFullName.ColumnName = "FullName";
                            dcFullName.Expression = "Apellido + ', ' + Nombre";
                            dt.Columns.Add(dcFullName);
                            clienteComboBox.DataSource = dt;
                            clienteComboBox.DisplayMember = "FullName";
                            clienteComboBox.ValueMember = "id";
                          
                        }
                      
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }


        private void cargarChofer()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_choferHabilitado";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            DataColumn dcFullName = new DataColumn();
                            dcFullName.DataType = System.Type.GetType("System.String");
                            dcFullName.ColumnName = "FullName";
                            dcFullName.Expression = "Apellido + ', ' + Nombre";
                            dt.Columns.Add(dcFullName);
                            choferComboBox.DataSource = dt;
                            choferComboBox.DisplayMember = "FullName";
                            choferComboBox.ValueMember = "id";

                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        
        private void RegistrarViajes_Load(object sender, EventArgs e)
        {

        }

        private void cambiaTipoViaje(){
            if (tipoViajeComboBox.Text == "Calle")
            {
                clienteComboBox.Text = null;
                clienteComboBox.Enabled = false;
            }
            else { clienteComboBox.Enabled = true; }
        }

        private void resgistrarViajeSql()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        int telefono = Convert.ToInt32(cantidadFichasTextBox.Text);
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_registroViaje";
                        command.Parameters.AddWithValue("@tipoViaje", tipoViajeComboBox.Text);
                        if(clienteComboBox.Text != "")command.Parameters.AddWithValue("@id_cliente", Convert.ToInt32(clienteComboBox.SelectedValue));
                        else command.Parameters.AddWithValue("@id_cliente", DBNull.Value);
                        command.Parameters.AddWithValue("@id_chofer", Convert.ToInt32(choferComboBox.SelectedValue));
                        command.Parameters.AddWithValue("@cantidadFichas", Convert.ToInt32(cantidadFichasTextBox.Text));
                        command.Parameters.AddWithValue("@id_turno", Convert.ToInt32(turnoComboBox.SelectedValue));
                        command.Parameters.AddWithValue("@fechaHora", Convert.ToDateTime(fechaHoraPicker.Value));
                        command.ExecuteReader();
                        MessageBox.Show("Operacion realizada con exito");
                        connection.Close();
                        this.Close();

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void tipoViajeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cambiaTipoViaje();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.resgistrarViajeSql();
        }
    }
}
